package com.ledtechdesign.service.api;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import com.ledtechdesign.beans.Item;
import com.ledtechdesign.beans.api.IAccountInfo;
import com.ledtechdesign.beans.api.IMeetingInfo;
import com.ledtechdesign.beans.api.IMeetingInfoDescriptor;
import com.ledtechdesign.beans.api.IRepository;
import com.ledtechdesign.beans.api.xml.AccountInfoXML;
import com.ledtechdesign.beans.api.xml.MeetingInfoDescriptorXML;
import com.ledtechdesign.beans.api.xml.MeetingInfoXML;
import com.ledtechdesign.beans.api.xml.RepositoryXML;
import com.ledtechdesign.service.ContainerType;
import com.ledtechdesign.service.IAFCSService;
import com.ledtechdesign.service.Utils;

public class AFCSAPIXMLResult implements IAFCSServiceResult {

	public IAccountInfo createAccountInfo(InputStream is) throws Exception {

		Element e = Utils.parseXML(is);
		final IAccountInfo ai = new AccountInfoXML(e);
		return ai;
	}

	public List<Item> createItemsList(InputStream data, ContainerType type)
			throws Exception {

		List<Item> items = new ArrayList<Item>();
		// (Element) listItems(data);

		// JXPathContext context = JXPathContext.newContext(repository);
		InputSource is = new InputSource(data);
		XPath xpath = XPathFactory.newInstance().newXPath();

		NodeList nodes = (NodeList) xpath.evaluate("/repository/children/node",
				is, XPathConstants.NODESET);

		for (int i = 0; i < nodes.getLength(); i++) {
			Element n = (Element) nodes.item(i);
			String name = n.getElementsByTagName("name").item(0)
					.getTextContent().trim();
			String desc = null;
			Date created = null;
			NodeList properties = n.getElementsByTagName("property");
			for (int j = 0; j < properties.getLength(); j++) {
				Element p = (Element) properties.item(j);
				if (p.getAttribute("name").equals("cr:description")) {
					desc = p.getTextContent().trim();
				} else if (p.getAttribute("name").equals("jcr:created")) {
					String raw = p.getTextContent().trim();
					// ends with "-HH:MM" and we need to turn this into
					// " -HHMM" so that it will parse!
					raw = raw.substring(0, raw.length() - 6) + " "
							+ raw.substring(raw.length() - 6, raw.length() - 3)
							+ raw.substring(raw.length() - 2);
					created = new java.text.SimpleDateFormat(
							"yyyy-MM-dd'T'HH:mm:ss.S Z").parse(raw);
				}
			}
			Item a = new Item(name, desc, created,
					type == IAFCSService.TEMPLATE_ITEMS);

			items.add(a);
		}
		return items;
	}

	public IMeetingInfo createMeetingInfo(InputStream is) throws Exception {

		Element e = Utils.parseXML(is);
		IMeetingInfo mi = new MeetingInfoXML(e);
		return mi;
	}

	public IMeetingInfoDescriptor createMeetingInfoDescriptor(InputStream d)
			throws Exception {
		// TODO Auto-generated method stub

		Element e = Utils.parseXML(d);
		IMeetingInfoDescriptor mi = new MeetingInfoDescriptorXML(e);
		return mi;
	}

	public IRepository createRepository(InputStream data) throws Exception {
		// TODO Auto-generated method stub

		Element e = Utils.parseXML(data);
		IRepository r = new RepositoryXML(e);
		return r;
	}

	public IRepository delete(InputStream i) throws Exception {
		// TODO Auto-generated method stub
		Element e = Utils.parseXML(i);
		IRepository r = new RepositoryXML(e);
		return r;
	}

}
